package com.qfhd.soft.cjzcore.UI.Dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qfhd.soft.cjzcore.Core.CJDpSpDxTransform;
import com.qfhd.soft.cjzcore.Core.CJLogger;
import com.qfhd.soft.cjzcore.R;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;

public class CJSignDialog {
    private AlertDialog dialog;

    private TextView coin_number;
    private TextView btn;
    private ImageView appIcon;
    private ImageView typeIcon;
    private TextView TX_title;
    private TextView TX_subtitle;
    private FrameLayout appLayout;
    private Button closeBtn;

    private Context mContext;
    private Activity mActivity;

    private String coinNumber;

    public CJSignDialog(Activity activity, Context context, String coinNumber){
        this.mActivity = activity;
        this.mContext = context;
        this.coinNumber = coinNumber;
    }

    public CJSignDialog(Activity activity, Context context, int coinNumber){
        this.mActivity = activity;
        this.mContext = context;
        this.coinNumber = "" + coinNumber;
    }

    public void init(){
        View dialogView = LayoutInflater.from(mActivity).inflate(R.layout.sign_dialog_layout, null);

        coin_number     = dialogView.findViewById(R.id.sign_dialog_coin);
        appIcon         = dialogView.findViewById(R.id.sign_dialog_appIcon);
        typeIcon        = dialogView.findViewById(R.id.sign_type_icon);
        btn             = dialogView.findViewById(R.id.sign_dialog_btn);
        TX_title        = dialogView.findViewById(R.id.sign_dialog_app_title);
        TX_subtitle     = dialogView.findViewById(R.id.sign_dialog_app_subtitle);
        appLayout       = dialogView.findViewById(R.id.sign_dialog_Ad_layout);
        closeBtn        = dialogView.findViewById(R.id.sign_dialog_close);

        dialog = new AlertDialog.Builder(this.mActivity)
                .setCancelable(false)
                .setView(dialogView)
                .create();
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

        coin_number.setText(coinNumber);
        closeBtn.setOnClickListener(v -> dismiss());
        btn.setOnClickListener(v -> dismiss());
    }

    public void setCloseBtnOnClickListerner(View.OnClickListener listerner){
        if(listerner != null) {
            closeBtn.setOnClickListener(listerner);
        }
    }

    public void setBtnOnClickListerner(View.OnClickListener listerner){
        if(listerner != null) {
            btn.setOnClickListener(listerner);
        }
    }

    public void setAppIcon_url(String iconUrl){
        Glide.with(mContext).load(iconUrl).placeholder(R.drawable.placeholder).into(appIcon);
    }

    public void setAppIcon(int imageId){
        appIcon.setImageResource(imageId);
    }

    public void setTypeIcon(int imageId){
        typeIcon.setImageResource(imageId);
    }

    public void setTitle(String title){
        TX_title.setText(title);
    }

    public void setSubTitle(String subTitle){
        TX_subtitle.setText(subTitle);
    }

    public void setBtnText(String text){
        btn.setText(text);
    }

    public void HideAppLayout(){
        appLayout.setVisibility(View.GONE);
    }

    public void hideAppLayoutChilds(){
        appIcon.setVisibility(View.GONE);
        TX_subtitle.setVisibility(View.GONE);
        TX_title.setVisibility(View.GONE);
    }

    public void showAppLayoutChilds(){
        appIcon.setVisibility(View.VISIBLE);
        TX_subtitle.setVisibility(View.VISIBLE);
        TX_title.setVisibility(View.VISIBLE);
    }

    public void show(){
        dialog.show();
    }

    public void dismiss(){
        dialog.dismiss();
    }

    public FrameLayout getAppLayout(){
        return appLayout;
    }

    public int getWidth(){
        CJLogger.v("sign dialog adLayout Width :"+ CJDpSpDxTransform.px2dip(appLayout.getLayoutParams().width));
        int width = QMUIDisplayHelper.getScreenWidth(mContext) - QMUIDisplayHelper.dp2px(mContext,12*2 + 12*2);
        CJLogger.v("sign dialog adLayout Width :"+ CJDpSpDxTransform.px2dip(width));
        return CJDpSpDxTransform.px2dip(width);
    }

    public int getHieght(){
        CJLogger.v("sign dialog adLayout height:" + CJDpSpDxTransform.px2dip(appLayout.getLayoutParams().height));
        return CJDpSpDxTransform.px2dip(appLayout.getLayoutParams().height);
    }

    public void hideDialog(){
        if(dialog.isShowing()){
            dialog.hide();
        }
    }

    public void openAdFrame(){
        appLayout.setVisibility(View.VISIBLE);
    }

    public void closeAdFrame(){
        appLayout.setVisibility(View.GONE);
    }
}
